#pragma once

#include <boost/scoped_array.hpp>

namespace cge {
	template< class T >
	struct DynamicArray: boost::noncopyable {
		typedef T        value_type;
		typedef T*       iterator;
		typedef const T* const_iterator;

		DynamicArray(): size_(0) {}
		explicit DynamicArray( unsigned n ) { realloc(n); }
		
		iterator begin() { return array_.get(); }
		iterator end()   { return array_.get()+size_; }

		template< class Collection >
		void fromCollection( const Collection& col ) {
			realloc(col.size());
			auto j = array_.get();
			for(auto i=col.begin(); i!=col.end(); ++i)
				*(j++) = *i;
		}

		void realloc( unsigned n ) {
			if( n )
				array_.reset( new T[size_=n] );
			else {
				array_.reset();
				size_ = 0;
			}
		}

		unsigned size() const {
			return size_;
		}

		bool     empty() { return size_==0; }

		T* get() {
			return array_.get();
		}

		operator T*() {
			return get();
		}
		
	private:
		unsigned               size_;
		boost::scoped_array<T> array_;
	};
}